﻿using System;
using System.Text;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using com.tiyma.common;
using com.tiyma.components;
using com.tiyma.httpmoudle;
using com.tiyma.model;
using System.Collections.Generic;

namespace com.tiyma.web
{
    public class PageUtil
    {

        /// <summary>
        /// 用户登录Token
        /// </summary>
        protected static UserToken Token
        {
            get
            {
                UserToken token = HttpContext.Current.Items[AuthenticateModule.CONTEXT_ROLE_KEY] as UserToken;
                return token;
            }

        }

        /// <summary>
        /// 执行脚本
        /// </summary>
        /// <param name="i_proc_name"></param>
        /// <param name="i_script"></param>
        /// <returns></returns>
        public static string ExcuteScript(string i_proc_name, string i_script,out bool o_ret,out string o_js)
        {
            o_ret = false;
            o_js = string.Empty;
            if (string.IsNullOrEmpty(i_proc_name) || string.IsNullOrEmpty(i_script))
                return "参数错误";
            string t_result = "操作失败";
            UserManager t_user_mgr = new UserManager();

            switch (i_proc_name)
            {
                case "register":
                    t_result = t_user_mgr.RegisterUser(i_script);
                    break;
                case "checkuser":
                    t_result = t_user_mgr.CheckUserExists(i_script);
                    break;
                case "login":
                    t_result = t_user_mgr.LoginUser(i_script);
                    break;
                case "updateuser":
                    if (Token == null)
                    {
                        t_result = "登录信息已经丢失,请重新登录!";
                    }
                    else
                    {
                        t_result = t_user_mgr.UpdateUser(i_script, Token.UserId);
                    }
                    break;
                case "bookcase":
                    if (Token == null)
                    {
                        t_result = "您还没登录，请登录后才能把书加入书架!";
                    }
                    else
                    {
                        BookManager t_book_mgr = new BookManager();
                        t_result = t_book_mgr.AddBookCase(Token.UserId, i_script);
                    }
                    break;
                case "userbook":
                    t_result = GetUserBook(1);
                    break;
                case "curreadbook":
                    t_result = GetUserBook(0);
                    break;
                case "downbook":
                    t_result = DownBook(i_script);
                    break;
                case "addsite":
                     BookManager t_book_mgr2 = new BookManager();
                     t_result = t_book_mgr2.AddUserSite(i_script);
                     break;
                case "bookclick":
                     BookManager t_book_mgr3 = new BookManager();
                    t_result=  t_book_mgr3.AddBookClick(i_script);
                     break;
                case "seochapter":
                     ChapterContentManager t_book_chapter_mgr2 = new ChapterContentManager();
                     t_result = t_book_chapter_mgr2.SEOChapter(i_script);
                     break;
                case "seonchapter":
                     ChapterContentManager t_book_chapter_mgr3 = new ChapterContentManager();
                     t_result = t_book_chapter_mgr3.SEONChapter(i_script);
                     break;

            }
            return t_result;
        }

        /// <summary>
        /// 下架小说
        /// </summary>
        /// <param name="i_book_name_list"></param>
        /// <returns></returns>
        private static string DownBook(string i_book_name_list)
        {
            string t_type = "1";//书架小说
            string[] t_book_name_arr = i_book_name_list.Split(new char[] { '%' }, StringSplitOptions.RemoveEmptyEntries);

            List<string> t_book_list = new List<string>();
            foreach (string t_book_name in t_book_name_arr)
            {
                string[] t_book_arr = t_book_name.Split(new char[] { '#' }, StringSplitOptions.RemoveEmptyEntries);
                t_type = t_book_arr[1];
                t_book_list.Add(t_book_arr[0]);

            }
            BookManager m_book_mgr = new BookManager();
            try
            {
                if (t_type == "1")
                {
                    bool t_ret = m_book_mgr.DownBook(t_book_list, t_type, Token.UserId);
                    if (t_ret)
                    {
                        return "操作成功";

                    }
                    else
                    {
                        return "操作失败";
                    }
                }
                else
                {
                    CookieManager.DeleteReadBook(t_book_list);
                    return "操作成功!";
                }

             
            }
            catch (Exception ex)
            {
                LogHelper.Error("下架书架小说失败" + ex.ToString());
                return "操作失败";
            }
        }

        /// <summary>
        /// 获取用户书架小说
        /// </summary>
        /// <returns></returns>
        private static string GetUserBook(int i_type)
        {
            string t_ret = "var b={0};";
            string t_result_template = "msg=%{0}%,book=%{1}%";
            if (Token == null)
                return string.Format(t_ret, "{"+string.Format(t_result_template, "您还没登录，请先登录!", "abc") +"}");

            BookManager t_book_mgr = new BookManager();
            List<BookInfo> t_list =null;

            if (i_type == 1)
            {
                t_list = t_book_mgr.GetUserBook(Token.UserId);
            }
            else
            {
                t_list = t_book_mgr.GetCurReadBook();
            }

            return ParseBook(t_list);
        }

        private static string ParseBook(List<BookInfo> t_list)
        {
            string t_ret = "var b={0};";
            string t_result_template = "msg=@{0}@,book={1}}";
            try
            {

                if (t_list == null || t_list.Count <= 0)
                    return string.Format(t_ret,"{"+ string.Format(t_result_template, "没有记录!", "null")+"}");

                StringBuilder t_sb = new StringBuilder();

                t_sb.AppendFormat("var bookArr = new Array({0});", t_list.Count);
                string m_item_template = "bookArr[{0}] = new Array(@{1}@,@{2}@,@{3}@,@{4}@,@{5}@,@{6}@,@{7}@,@{8}@);";
                for (int index = 0; index < t_list.Count; index++)
                {
                    t_sb.AppendFormat(m_item_template
                        , index
                        , t_list[index].BookCategory
                        , t_list[index].BookName
                        , t_list[index].LastUpdateChapterId
                        , t_list[index].LastUpdateChapterName
                        , t_list[index].LastUpdateTime
                        , t_list[index].AuthorName
                        , t_list[index].SiteName
                        , t_list[index].Status);

                }

                return t_sb.ToString() + "\r\n" + string.Format(t_ret,"{"+ string.Format(t_result_template, "成功", "bookArr")+"}");
            }
            catch (Exception ex)
            {
                LogHelper.Error("获取用户小说失败" + ex.ToString());
                return string.Format(t_ret, "{"+string.Format(t_result_template, "出现异常,请稍后重试!", "null")+"}");
            }

        }
    }
}
